package quartz.job.batch;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
// @Configuration(proxyBeanMethods = false)
public class Sample extends Base<Integer, Integer> {
    final JdbcTemplate jdbcTemplate;
    final String NAME = "Sample";

    // @JobScope
    @StepScope
    @Bean(NAME + "Reader")
    protected ItemReader<Integer> reader(@Value("#{jobParameters}") Map<String, Object> jobParameters,
                                         @Value("#{jobExecutionContext}") Map<String, Object> jobExecutionContext,
                                         @Value("#{stepExecutionContext}") Map<String, Object> stepExecutionContext) {
        List<Integer> items = new LinkedList<>();
        for (int i = 0; i < 3; i++) {
            log.info("reader data: " + i);
            items.add(i);
        }
        return super.reader(items);
    }

    @Override
    protected Integer processor(Integer item) {
        log.info("processor data: " + item++);
        return item;
    }

    @Override
    protected void writer(List<? extends Integer> items) {
        items.forEach(item -> log.info("write data: " + item));
    }

    @Bean(NAME + "Step")
    @Override
    protected Step step(@Qualifier(NAME + "Reader") ItemReader<Integer> reader) {
        return super.step(reader);
    }

    @Bean(NAME + "Job")
    @Override
    protected Job job(@Qualifier(NAME + "Step") Step step) {
        return super.job(step);
    }
}